﻿using UnityEngine;
using System.Collections;
using System;

public class UMShareAndroid : TJLogger
{

	protected AndroidJavaObject jo = null;
	protected Action<bool> splashCallBack = null;

	///WEIXIN_appid, WEIXIN_secret,
	/// QQ_appid, QQ_secret,
	/// SINA_appid, SINA_secret
	public void Init (params string[] key)
	{
		TJLog ("友盟分享初始化");
		jo = new AndroidJavaObject ("com.tjum.Social");
//		jo.Call ("setKey", Platform.WEIXIN.ToString(),key[0], key[1]);
//		jo.Call ("setKey", Platform.QQ.ToString(),key[2], key[3]);
//		jo.Call ("setKey", Platform.SINA.ToString(),key[4], key[5]);
		jo.Call ("setKey", key [0], key [1], key [2], key [3], key [4], key [5]);

		jo.Call ("setCallBack", "TJSDK", "OnShareResult");
	}

	public void Share (string title, string content, string imageUri, string shareUrl, Action<bool> act = null)
	{
		splashCallBack = act;
		jo.Call ("Share", title, content, imageUri, shareUrl);
	}

	public void Share (string title, string content, string imageUri, string shareUrl, bool isAll, Action<bool> act = null)
	{
		splashCallBack = act;
		jo.Call ("Share", title, content, imageUri, shareUrl, isAll);
	}

	public void Share (Platform platform, string title, string content, string imageUri, string shareUrl, Action<bool> act = null)
	{
		splashCallBack = act;
		jo.Call ("Share", platform.ToString (), title, content, imageUri, shareUrl);
	}

	public void SetShare (Platform platform, string title, string content, string imageUri, string shareUrl)
	{
		jo.Call ("SetShare", platform.ToString (), title, content, imageUri, shareUrl);
	}

	public void OnShareResult (string str)
	{
		TJLog ("分享回调: " + str);
		string[] datas = str.Split ('|');
		switch (datas [1]) {
		case "onStart":
			TJLog (datas [0] + "分享开始");
			break;
		case "onResult":
			TJLog (datas [0] + "分享成功");
			if (splashCallBack != null) {
				splashCallBack (true);
			}
			break;
		case "onCancel":
			TJLog (datas [0] + "分享取消");
			if (splashCallBack != null) {
				splashCallBack (false);
			}
			break;
		case "onError":
			TJLog (datas [0] + "分享错误：" + datas [2]);
			if (splashCallBack != null) {
				splashCallBack (false);
			}
			break;
		default :
			TJLog ("未知分享状态");
			break;
		}
	}
}
